package com.diy.springframework.step.loader;

import cn.hutool.core.lang.Assert;
import com.diy.springframework.step.resource.ClassPathResource;
import com.diy.springframework.step.resource.FileSystemResource;
import com.diy.springframework.step.resource.Resource;
import com.diy.springframework.step.resource.UrlResource;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * @program: diy_spring
 * @description:
 * @author: CaoYong
 * @create: 2021-07-22 09:57
 **/
public class DefaultResourceLoader implements ResourceLoader {

    @Override
    public Resource getResource(String location) {
        Assert.notNull(location, "Location must not null");
        if (location.startsWith(CLASSPATH_URL_PREFIX)) {
            return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()));
        } else {
            try {
                URL url = new URL(location);
                return new UrlResource(url);
            } catch (MalformedURLException e) {
                return new FileSystemResource(location);
            }
        }

    }
}
